﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Entities;
using Home.MobileSales.ImpExpData.View;
using Home.MobileSales.Sales.Interface;
using Home.MobileSales.Common.Models;
using Home.MobileSales.Sales.View;
using System.Windows.Forms;
using Home.MobileSales.WorkGps;

namespace Home.MobileSales.Sales.Presenter
{
    class MainFormPresenter
    {
        private IMainFormView _view;
        private SplashWindowForm splashForm;

        public MainFormPresenter(IMainFormView view)
        {
            _view = view;
            splashForm = new SplashWindowForm();
            splashForm.Owner = (Form) _view;
            splashForm.Show();
            Application.DoEvents();
            _view.EnableForm = false;
            new SplashFormPresenter(splashForm);
            _view.InitializedComponents += initializeForm;
            _view.LoadedForm += LoadForm;
        }

        private void initializeForm(object sender, EventArgs e)
        {
            try
            {
                _view.LoadedForm += LoadForm;
                _view.ChangeDataWork += ChangeDataWork;
                _view.DateWork = DateTime.Now.Date;
                _view.SelectCheckVisitAction += CheckVisit;

                _view.SelectCreateOrderAction += CreateOrder;
                _view.EditOrderAction += EditOrder;
                _view.DeleteOrderAction += DeleteOrder;
                _view.DetailsOrderAction += DetailsOrder;

                _view.SelectGrapVisitAction += GrapicsVisit;
                _view.SelectSettingsAction += ShowSettings;
                _view.SelectBuildVisitAction += DailyVisits;

                _view.SelectImportDataAction += ImportData;
                _view.SelectExportDataAction += ExportData;

                _view.SelectAboutAction += AboutAction;
            }
            catch(Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }

        }

        public void LoadForm(object sender, EventArgs e)
        {
            _view.EnableForm = true;
            _view.VizibleForm = true;
            splashForm.Close();
        }

        public void DailyVisits(object sender, EventArgs e)
        {
           new DailyVisitForm(_view.DateWork).ShowDialog();
            _view.DataSource = DataManager.GetInstance().Visists.GetVisitsByDate(_view.DateWork, false);
        }

        public void ShowSettings (object sender, EventArgs e)
        {
            new SettingsForm().ShowDialog();
        }

        public void GrapicsVisit (object sender, EventArgs e)
        {
            new SheduleVisitsForm().Show();
        }

        public void ChangeDataWork (object sender, EventArgs e)
        {
            try
            {
                List<Visit> visits = DataManager.GetInstance().Visists.GetVisitsByDate(_view.DateWork, false);
                _view.DataSource = visits;
            }
            catch(Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void CheckVisit (object sender, EventArgs e)
        {
            try
            {
                Visit currentVisit = ((Visit) _view.Current);
                if (Settings.GetInstance().EnableGPS)
                {
                    currentVisit.GpsPosition = GpsWorker.GetInstance().GpsData.ToString();
                    currentVisit.IsVisit = true;
                    currentVisit.TimeEndVisit = DateTime.Now.ToString();
                    DataManager.GetInstance().Visists.UpdateVisit(currentVisit);
                }
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void CreateOrder (object sender, EventArgs e)
        {
            try
            {
                Visit visit = ((Visit) _view.Current);
                if (visit.TimeEndVisit == string.Empty)
                {
                    if (MessageBox.Show("Відмітити візит ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        CheckVisit(null, null);
                    }
                }
                Order tempOrder = new Order();
                tempOrder.DateOrder = DateTime.Now.Date;
                tempOrder.NotDelivery = Settings.GetInstance().IsVanSales;
                string number = string.Empty;
                if (Settings.GetInstance().AutoGenerateNumber)
                {
                    Settings.GetInstance().NumberDocument++;
                    number = Settings.GetInstance().NumberDocument.ToString();
                }
                tempOrder.NumberOrder = number;
                tempOrder.SkladId = Settings.GetInstance().DefaultSklad;
                tempOrder.VisitId = visit.IdVisit;
                tempOrder.IsReadOnly = false;

                new OrderForm(
                    DataManager.GetInstance().Orders.CreateOrder(tempOrder), visit.BuyPoint, true).ShowDialog();
                ChangeDataWork(null, null);
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void EditOrder (object sender, EventArgs e)
        {
            Visit visit = ((Visit)_view.Current);
            new OrderForm(_view.CurrentOrder, visit.BuyPoint, true).ShowDialog();
            ChangeDataWork(null, null);
        }

        public void DeleteOrder (object sender, EventArgs e)
        {
            DataManager.GetInstance().Orders.DeleteOrder((_view.CurrentOrder).IdOrder);
            ChangeDataWork(null, null);
        }

        public void DetailsOrder(object sender, EventArgs e)
        {
            Visit visit = ((Visit)_view.Current);
            new OrderForm(_view.CurrentOrder, visit.BuyPoint, false).ShowDialog();
            ChangeDataWork(null, null);
        }

        public void ImportData (object sender, EventArgs e)
        {
            new ImportForm(true).ShowDialog();
            ChangeDataWork(null, null);
        }

        public void ExportData (object sender, EventArgs e)
        {
            new ImportForm(false).ShowDialog();
            ChangeDataWork(null, null);
        }

        public void AboutAction (object sender, EventArgs e)
        {
            new AboutForm().ShowDialog();
        }
    }
}
